package com.definesys.smecrailtransit.adapter.SelctLineAdapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.adapter.SelectMonthAdapter.SelectMonthAdapter;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.FragmentMonthFormBinding;
import com.definesys.smecrailtransit.databinding.LayoutSelectLineItemBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormFragment;
import com.definesys.smecrailtransit.module.RealStatus.RealStatusFragment;
import com.definesys.smecrailtransit.module.TotalTime.TotalTimeFragment;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.definesys.smecrailtransit.utils.ToastyUtils;
import com.hwangjr.rxbus.Bus;

import java.util.ArrayList;

/**
 * Created by cjy on 2017/7/27.
 */

public class SelectLineAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {
    private int SELECT = 1;
    private int UNSELECT = 0;
    private String type;
    private ArrayList<String> list;
    protected Bus mRxbus;

    public SelectLineAdapter(ArrayList<String> list, Bus mRxbus, String type){
        if(list == null){
            this.list = new ArrayList<>();
        }else{
            this.list = list;
        }

        this.type = type;
        this.mRxbus = mRxbus;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutSelectLineItemBinding layoutLineBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.layout_select_line_item,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutLineBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutLineBinding);
        return smecBaseRecyclerViewHolder;


    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        LayoutSelectLineItemBinding viewDataBinding=(LayoutSelectLineItemBinding)holder.getBinding();
        if(holder.getViewType() == SELECT){
            int l=list.get(position)!=null ? list.get(position).length()-2:0;
            viewDataBinding.text.setText(list.get(position).substring(0,l));
            viewDataBinding.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(SelectLineAdapter.this.type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                        SelectLineAdapter.this.mRxbus.post(MonthFormFragment.CHANGE_LINE,false);
                    }
                    if(SelectLineAdapter.this.type.equals(SmecRailApi.MonthCode.TOTALTIME)){
                        SelectLineAdapter.this.mRxbus.post(TotalTimeFragment.CHANGE_LINE,false);
                    }

                }
            });

        }else{
            int l=list.get(position)!=null ? list.get(position).length()-2:0;
            viewDataBinding.text.setText(list.get(position).substring(0,l));
            //viewDataBinding.item.setBackgroundResource(R.color.monthForm_line_grey);
            viewDataBinding.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(SelectLineAdapter.this.type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                        SelectLineAdapter.this.mRxbus.post(MonthFormFragment.CHANGE_LINE,true);
                    }
                    if(SelectLineAdapter.this.type.equals(SmecRailApi.MonthCode.TOTALTIME)){
                        SelectLineAdapter.this.mRxbus.post(TotalTimeFragment.CHANGE_LINE,true);
                    }
                    if(SelectLineAdapter.this.type.equals(SmecRailApi.MonthCode.REALSTATUS)){
                        SelectLineAdapter.this.mRxbus.post(RealStatusFragment.CHANGE_LINE,true);
                    }
                }
            });



        }

        switch (list.get(position)){
            case "1号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_one);break;
            case "2号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_two);break;
            case "3号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_three);break;
            case "4号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_four);break;
            case "5号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_five);break;
            case "9号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_nine);break;
            case "12号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_twelve);break;
            case "16号线": viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_sixteen);break;
            default: viewDataBinding.lineBackground.setBackgroundResource(R.drawable.border_circle_radius_twelve);
        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(list.get(position).equals("12号线")){
            return SELECT;
        }else {
            return UNSELECT;
        }
    }
}
